/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-08-14
 * Time: 19:01
 */
public class CirculateMyQueue {
    private int[] arr;
    private int front;
    private int rear;
    private boolean mark = true;

    //构造方法
    public CirculateMyQueue(int k) {
        this.arr = new int[k];
    }

    //入队列
    public boolean enQueue(int data) {
        if (isFull()) {
            return false;
        }
        this.arr[this.rear] = data;
        this.rear = (this.rear + 1) % this.arr.length;
        this.mark = true;
        if (this.rear == this.front) {
            this.mark = false;
        }
        return true;
    }

    //出队列
    public boolean deQueue( ) {
        if (isEmpty()) {
            return false;
        }
        this.front = (this.front + 1) % this.arr.length;
        this.mark = true;
        return true;
    }

    //从队首获取元素。如果队列为空，返回 -1 。
    public int Front() {
        if (isEmpty()) {
            return -1;
        }
        return this.arr[this.front];
    }

    //获取队尾元素。如果队列为空，返回 -1 。
    public int Rear() {
        if (isEmpty()) {
            return -1;
        }
        int ans = (this.rear == 0?this.arr.length - 1:this.rear - 1);
        return this.arr[ans];
    }

    //检查循环队列是否为空
    public boolean isEmpty() {
        return (this.mark == true && this.front == this.rear);
    }

    //检查循环队列是否已满
    public boolean isFull() {
        return (this.mark == false && this.front == this.rear);
    }
}
